package com.atguigu.yygh.msm.service.impl;/*
 * @author: XueYouPeng
 * @time: 2023/6/13 18:29
 */

import com.atguigu.exphandler.YyghException;
import com.atguigu.utils.HttpUtils;
import com.atguigu.yygh.msm.service.MsmService;
import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class MsmServiceImpl implements MsmService {

    @Autowired
    StringRedisTemplate redisTemplate;


    @Override
    public void sendCode(String phone) {

        //1.先判断该手机号是否存在有效期之内的验证码
        String code = redisTemplate.opsForValue().get(phone);
        if(!StringUtils.isEmpty(code)){
            return;
        }

        //2、调用第三方短信接口实现短信验证码的发送
        String host = "https://dfsns.market.alicloudapi.com";
        String path = "/data/send_sms";
        String method = "POST";


        String appcode = "1d57485f507e4f0291625fffb907c399";

        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 1d57485f507e4f0291625fffb907c399
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> bodys = new HashMap<String, String>();

        //生成一个随机4位字符串
        String s = UUID.randomUUID().toString().replaceAll("-", "");
        code = s.substring(0,4);

        bodys.put("content", "code:" + code);
        bodys.put("phone_number", phone);


        //短信模板id
        bodys.put("template_id", "CST_ptdie100");


        try {
            /**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
            //使用该工具类发起post类型的请求
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            System.out.println(response.toString());
            //响应状态码 200-成功
            int statusCode = response.getStatusLine().getStatusCode();
            //获取response的body
            //System.out.println(EntityUtils.toString(response.getEntity()));

            //3、发送成功，验证码存储redis中5分钟
            if (statusCode == 200) {
                //发送成功  k-v 存入到redis
                redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);
            }else {
                throw new YyghException(20001,"验证码发送失败");
            }

        } catch (Exception e) {
            throw new YyghException(20001,"验证码发送失败");
        }
    }

}
